import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '标本条码',		name: 'spec_code' },
			{ label: '沟通类型',		name: 'type', 		type: 'select', },
			{ label: '状态',			name: 'status',		type: 'select', loading:true },
			{ label: '时间',  		names: ['start_date','end_date'], type:'date-range',format:1 },
		],
		// 提交表单列表
		formData : [
			{ label: '标本条码',		name: 'spec_code', 	half:true, 	required:true, onValue:async ({value, formData})=>{
				if($fn.isNumber(+value) && (value.length === 12 || value.length === 14)){
					// 自然项目
					await this.getNature({spec_code:value}, true)
					
					formData[2].disabled = false
					this.setState({ formData })
				}
			} },
			{ label: '送检单位',		name: 'inspec_unit_id', 	half:true, 	required:true, type: 'select',  onValue:({ rows })=>{
				if(rows){
					this.submit = { ...this.submit, inspec_unit: rows.name }
				}
			} },
			{ label: '自然项目',		name: 'kind_id', 			full:true, 	required:true, type: 'select', p:'请先输入条码', disabled:true, nameStr:'kind_name', idStr:'kind_id', onValue:({value, ref, rows})=>{
				if(rows){
					this.submit = { ...this.submit, ...rows }
					const spec_code = ref.getName('spec_code')
					$http.submit(null,'sp-customer-log/kindId', { param:{ kind_id:value, spec_code } }).then(data=>{
						ref.setValue({major_group: data[0].major_group_id})
					})
				}else{
					ref.reset(['major_group'])
				}
			}},
			{ label: '送检科室',		name: 'depart_name', 		half:true, 	required:true},
			{ label: '岗位',			name: 'major_group', 		half:true, 	required:true, type: 'select' },
			{ label: '回顾',			name: 'look_back', 			half:true,	required:true, type: 'select' },
			{ label: '沟通类型',		name: 'type', 				half:true, 	required:true, type: 'select'},
			{ label: '反馈人',		name: 'contact_user', 		half:true, 	required:true, },
			{ label: '联系电话',		name: 'phone', 				half:true, 	required:true, },
			{ label: '联系日期',		name: 'contact_at', 		half:true, 	required:true, type:'date-time', format:1 },
			{ label: '反馈内容',     name: 'inspec_content', 	full:true, 	type:'textarea' },
			{ label: '客服处理',     name: 'contact_reason', 	full:true, 	type:'textarea' },
		]
	}
	
	model = { }
	query = { status:'' }
	submit = { }
	
	componentDidMount(){
		this.getSelect()
		
		if(window.isInit){
		  this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
		
		$cache.then( async f => {
			// 沟通类型
			const gouTypeData =  $fn.setRows([ forms, formData ], 'type', await f.communication_type)
			// 状态
			const statusData = $fn.setRow(forms, 'status',  await f.treatment_state)
			// 送检单位
			$fn.setRow(formData, 'inspec_unit_id', await f.getCache({api:'hospital'}))
			// 岗位
			$fn.setRow(formData, 'major_group', await f.getCache({api:'gang_wei'}))
			// 回顾
			$fn.setRow(formData, 'look_back',  await f.retrospect)
			//
			this.setState({ forms, gouTypeData, statusData })
		})
	}
	// 获取自然项目
	getNature = async (param, isAdd) => {
		// 自然项目
		await $http.pull(null,'sp-customer-log/getSpecInfo', { param, lading:true }).then(data=>{
			const { formData } = this.state
			formData[2].p = null
			formData[2].disabled = !$fn.hasArray(data)
			$fn.setRow(formData, 'kind_id', data)
		})
		// 送检单位
		if(isAdd){
			this.formRef.reset(['kind_id','inspec_unit_id','major_group'])
			await $http.pull(null,'/sp-customer-log/getInfoBySpec', { param, loading:true }).then(({spec_info})=>{
				if($fn.hasObject(spec_info)){
					const { inspec_unit_id, inspec_unit, major_group_id  } = spec_info
					const { comp_id, uuid, id } = $fn.getUser()
					this.submit = { ...this.submit, inspec_unit, comp_id, uuid, id }
					setTimeout(()=>{
						// this.formRef.setValue({ kind_id: kind_data.kind_id||null, inspec_unit_id:inspec_unit_id || null, major_group: major_group_id || null })
						this.formRef.setValue({ kind_id: null,inspec_unit_id:inspec_unit_id || null, major_group: major_group_id || null })
					})
				}else{
					setTimeout(()=>{
						this.formRef.setValue({ kind_id: null, inspec_unit_id:null, major_group: null })
					})
				}
			})
		}
	}
	// paging
	fetch = () => $fn.fetch.call(this,'sp-customer-log/index')
	
	// 表单
	cols = [
		{ title: '条码号',			dataIndex: 'spec_code', 		...$fn.table.spec_code },
		{ title: '自然项目', 		dataIndex: 'kind_name', 		width:250 },
		{ title: '沟通类型', 		dataIndex: 'type', 				width:150, render: text => $fn.getState(this.state.gouTypeData,text) },
		{ title: '岗位', 			dataIndex: 'project_name', 		width:150 },
		{ title: '反馈人',			dataIndex: 'contact_user', 		...$fn.table.name },
		{ title: '反馈日期', 		dataIndex: 'contact_at', 		...$fn.table.time },
		{ title: '客服处理', 		dataIndex: 'contact_reason', 	width:150 },
		{ title: '送检医院', 		dataIndex: 'inspec_unit', 		width:250 },
		{ title: '反馈电话',			dataIndex: 'phone', 			...$fn.table.phone },
		{ title: '送检科室', 		dataIndex: 'depart_name', 		width:150 },
		{ title: '反馈内容', 		dataIndex: 'inspec_content', 	width:150 },
		{ title: '回顾', 			dataIndex: 'look_back', 		width:150, render: text => +text === 1 ? '是' : '否' },
		{ title: '状态', 			dataIndex: 'status', 			width:150, render: text => $fn.getState(this.state.statusData,text) },
		TableControls.controls.call(this, { width: 150 })
	]
	// 表格操作按钮
	table_controls = ( text, { uuid, status }) => {
		return [		
			// 编辑
			+status === 0 ? TableControls.click.call(this, '编辑', ()=>{
				this.modal.open({ formRef:this.formRef })
				$http.submit(null,'sp-customer-log/info', { param:{ uuid }, isPull:true }).then(async data=>{
					const { formData } = this.state
					await this.getNature({spec_code:data.spec_code})
					// 获取自然项目字段
					let kind = {}
					const kind_name = $fn.hasArray(data.kind_name) ? data.kind_name[0] : null
					if($fn.hasArray(formData[2].data)){
						const arr = formData[2].data.filter( v => v.kind_name === kind_name )
						if($fn.hasArray(arr)){
							kind = arr[0]
						}
					}
					this.submit = { ...this.submit, ...kind, id: data.id, uuid }
					this.initSubmit = data
					
					formData[0].disabled = true
					this.setState({ ids: {uuid:data.uuid} , formData }, ()=>{
						setTimeout(()=> this.formRef.setValue({...data, ...kind}))
					})
				})
			}) : null,
			+status === 0 ? Confirm.call(this, '审核', 'sp-customer-log/audit', { param: uuid }) : null,	// 审核
			// 已处理
			+status === 1 ? Confirm.call(this, '完成', 'sp-customer-log/complete', { param: uuid }) : null
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this,{ click:()=>{
				const { formData } = this.state
				formData[0].disabled = false
				formData[2].p = '请先输入条码'
				formData[2].data = []
				this.setState({ formData })
			} }),
			Controls.exports.call(this, 'sp-customer-log/index')
		]
	}
	
	render(){
		return (
			<>
				<Page title='客户反馈' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { isInit: window.isInit }) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ labelWidth:'s5', api:'sp-customer-log/add', editApi:'sp-customer-log/edit' }) }
			</>
		)
	}
}

export default Index